home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Log / sql.php < prev    next >
PHP Script  |  2004-10-01  |  7KB  |  226 lines

  1. <?php
  2. /**
  3.  * $Header: /repository/pear/Log/Log/sql.php,v 1.34 2004/08/19 06:35:57 jon Exp $
  4.  * $Horde: horde/lib/Log/sql.php,v 1.12 2000/08/16 20:27:34 chuck Exp $
  5.  *
  6.  * @version $Revision: 1.34 $
  7.  * @package Log
  8.  */
  9.  
  10. /** PEAR's DB package */
  11. require_once 'DB.php';
  12.  
  13. /**
  14.  * The Log_sql class is a concrete implementation of the Log::
  15.  * abstract class which sends messages to an SQL server.  Each entry
  16.  * occupies a separate row in the database.
  17.  *
  18.  * This implementation uses PHP's PEAR database abstraction layer.
  19.  *
  20.  * CREATE TABLE log_table (
  21.  *  id          INT NOT NULL,
  22.  *  logtime     TIMESTAMP NOT NULL,
  23.  *  ident       CHAR(16) NOT NULL,
  24.  *  priority    INT NOT NULL,
  25.  *  message     VARCHAR(200),
  26.  *  PRIMARY KEY (id)
  27.  * );
  28.  *
  29.  * @author  Jon Parise <jon@php.net>
  30.  * @since   Horde 1.3
  31.  * @since   Log 1.0
  32.  * @package Log 
  33.  *
  34.  * @example sql.php     Using the SQL handler.
  35.  */
  36. class Log_sql extends Log {
  37.  
  38.     /** 
  39.      * Array containing the dsn information. 
  40.      * @var string
  41.      * @access private
  42.      */
  43.     var $_dsn = '';
  44.  
  45.     /** 
  46.      * Object holding the database handle. 
  47.      * @var object
  48.      * @access private
  49.      */
  50.     var $_db = null;
  51.  
  52.     /**
  53.      * Flag indicating that we're using an existing database connection.
  54.      * @var boolean
  55.      * @access private
  56.      */
  57.     var $_existingConnection = false;
  58.  
  59.     /** 
  60.      * String holding the database table to use. 
  61.      * @var string
  62.      * @access private
  63.      */
  64.     var $_table = 'log_table';
  65.  
  66.     /**
  67.      * String holding the name of the ID sequence.
  68.      * @var string
  69.      * @access private
  70.      */
  71.     var $_sequence = 'log_id';
  72.  
  73.     /**
  74.      * Maximum length of the $ident string.  This corresponds to the size of
  75.      * the 'ident' column in the SQL table.
  76.      * @var integer
  77.      * @access private
  78.      */
  79.     var $_identLimit = 16;
  80.  
  81.  
  82.     /**
  83.      * Constructs a new sql logging object.
  84.      *
  85.      * @param string $name         The target SQL table.
  86.      * @param string $ident        The identification field.
  87.      * @param array $conf          The connection configuration array.
  88.      * @param int $level           Log messages up to and including this level.
  89.      * @access public     
  90.      */
  91.     function Log_sql($name, $ident = '', $conf = array(),
  92.                      $level = PEAR_LOG_DEBUG)
  93.     {
  94.         $this->_id = md5(microtime());
  95.         $this->_table = $name;
  96.         $this->_mask = Log::UPTO($level);
  97.  
  98.         /* If a specific sequence name was provided, use it. */
  99.         if (!empty($conf['sequence'])) {
  100.             $this->_sequence = $conf['sequence'];
  101.         }
  102.  
  103.         /* If a specific sequence name was provided, use it. */
  104.         if (isset($conf['identLimit'])) {
  105.             $this->_identLimit = $conf['identLimit'];
  106.         }
  107.  
  108.         /* Now that the ident limit is confirmed, set the ident string. */
  109.         $this->setIdent($ident);
  110.  
  111.         /* If an existing database connection was provided, use it. */
  112.         if (isset($conf['db'])) {
  113.             $this->_db = &$conf['db'];
  114.             $this->_existingConnection = true;
  115.             $this->_opened = true;
  116.         } else {
  117.             $this->_dsn = $conf['dsn'];
  118.         }
  119.     }
  120.  
  121.     /**
  122.      * Opens a connection to the database, if it has not already
  123.      * been opened. This is implicitly called by log(), if necessary.
  124.      *
  125.      * @return boolean   True on success, false on failure.
  126.      * @access public     
  127.      */
  128.     function open()
  129.     {
  130.         if (!$this->_opened) {
  131.             $this->_db = &DB::connect($this->_dsn, true);
  132.             if (DB::isError($this->_db)) {
  133.                 return false;
  134.             }
  135.             $this->_opened = true;
  136.         }
  137.  
  138.         return $this->_opened;
  139.     }
  140.  
  141.     /**
  142.      * Closes the connection to the database if it is still open and we were
  143.      * the ones that opened it.  It is the caller's responsible to close an
  144.      * existing connection that was passed to us via $conf['db'].
  145.      *
  146.      * @return boolean   True on success, false on failure.
  147.      * @access public     
  148.      */
  149.     function close()
  150.     {
  151.         if ($this->_opened && !$this->_existingConnection) {
  152.             $this->_opened = false;
  153.             return $this->_db->disconnect();
  154.         }
  155.  
  156.         return ($this->_opened === false);
  157.     }
  158.  
  159.     /**
  160.      * Sets this Log instance's identification string.  Note that this
  161.      * SQL-specific implementation will limit the length of the $ident string
  162.      * to sixteen (16) characters.
  163.      *
  164.      * @param string    $ident      The new identification string.
  165.      *
  166.      * @access  public
  167.      * @since   Log 1.8.5
  168.      */
  169.     function setIdent($ident)
  170.     {
  171.         $this->_ident = substr($ident, 0, $this->_identLimit);
  172.     }
  173.  
  174.     /**
  175.      * Inserts $message to the currently open database.  Calls open(),
  176.      * if necessary.  Also passes the message along to any Log_observer
  177.      * instances that are observing this Log.
  178.      *
  179.      * @param mixed  $message  String or object containing the message to log.
  180.      * @param string $priority The priority of the message.  Valid
  181.      *                  values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  182.      *                  PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  183.      *                  PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  184.      * @return boolean  True on success or false on failure.
  185.      * @access public     
  186.      */
  187.     function log($message, $priority = null)
  188.     {
  189.         /* If a priority hasn't been specified, use the default value. */
  190.         if ($priority === null) {
  191.             $priority = $this->_priority;
  192.         }
  193.  
  194.         /* Abort early if the priority is above the maximum logging level. */
  195.         if (!$this->_isMasked($priority)) {
  196.             return false;
  197.         }
  198.  
  199.         /* If the connection isn't open and can't be opened, return failure. */
  200.         if (!$this->_opened && !$this->open()) {
  201.             return false;
  202.         }
  203.  
  204.         /* Extract the string representation of the message. */
  205.         $message = $this->_extractMessage($message);
  206.  
  207.         /* Build the SQL query for this log entry insertion. */
  208.         $id = $this->_db->nextId($this->_sequence);
  209.         $q = sprintf('insert into %s (id, logtime, ident, priority, message)' .
  210.                      'values(%d, CURRENT_TIMESTAMP, %s, %d, %s)',
  211.                      $this->_table, $id, $this->_db->quote($this->_ident),
  212.                      $priority, $this->_db->quote($message));
  213.  
  214.         $result = $this->_db->query($q);
  215.         if (DB::isError($result)) {
  216.             return false;
  217.         }
  218.  
  219.         $this->_announce(array('priority' => $priority, 'message' => $message));
  220.  
  221.         return true;
  222.     }
  223. }
  224.  
  225. ?>
  226.